package request

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
)

// ResponseToMap 解析response到结构体
func ResponseToData(resp *http.Response, data any) error {
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	// var result map[string]interface{}
	err = json.Unmarshal(body, &data)
	if err != nil {
		return err
	}
	return nil
}

func ResponseToMap(resp *http.Response) (map[string]interface{}, error) {
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	var result map[string]interface{}
	err = json.Unmarshal(body, &result)
	if err != nil {
		return nil, err
	}
	return result, nil
}
